package com.bytedance.scenedemo;

import com.bytedance.scene.Scene;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scenedemo.architecture_patterns.by_constructor.ByConstructorSample;
import com.bytedance.scenedemo.architecture_patterns.mvvm.ViewModelSceneAbility;
import com.bytedance.scenedemo.architecture_patterns.scope.ScopeSample;
import com.bytedance.scenedemo.restore.SupportRestoreAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

public class PatternsSamples extends Scene implements Component.ClickedListener {
    @Override
    public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        ComponentContainer componentContainer = (ComponentContainer) scatter.parse(ResourceTable.Layout_patterns_samples, container, false);
        initView(componentContainer);
        return componentContainer;
    }

    private void initView(ComponentContainer componentContainer) {
        Button btnScope = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_scope);
        Button btnConstructorPass = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_constructor_pass);
        Button btnMvvm = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_mvvm);
        Button btnUnidirectionalDataFlow = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_unidirectional_data_flow);
        Button btnViper = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_viper);
        btnScope.setClickedListener(this);
        btnConstructorPass.setClickedListener(this);
        btnMvvm.setClickedListener(this);
        btnUnidirectionalDataFlow.setClickedListener(this);
        btnViper.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        switch (id) {
            case ResourceTable.Id_btn_scope:
                NavigationSceneGetter.requireNavigationScene(this).push(ScopeSample.class);
                break;
            case ResourceTable.Id_btn_constructor_pass:
                NavigationSceneGetter.requireNavigationScene(this).push(ByConstructorSample.class);
                break;
            case ResourceTable.Id_btn_mvvm:
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getAbility().getBundleName())
                        .withAbilityName(ViewModelSceneAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                SceneExtensions.requireNavigationScene(this).startAbility(intent);
                break;
            case ResourceTable.Id_btn_unidirectional_data_flow:
            case ResourceTable.Id_btn_viper:
                showToast("TODO");
                break;
            default:
                break;
        }
    }
}
